// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.commons.ui.swt.table;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.Collator;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 */
public class ComparatorFactory {

    private static final Collator col = Collator.getInstance(Locale.getDefault());

    public static int UP_DOWN = 1;

    public static Comparator getComparatorFrom(Class clazz) {
        if (clazz == int.class) {
            return getIntComparator();
        } else if (clazz == Date.class) {
            return getDateComparator();
        } else if (clazz == Double.class) {
            return getDoubleComparator();
        } else if (clazz == Long.class) {
            return getLongComparator();
        } else if (clazz == Timestamp.class) {
            return getTimestampComparator();
        } else if (clazz == Time.class) {
            return getTimeComparator();
        } else {
            return getDefaultComparator();
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getTimeComparator".
     * 
     * @return
     */
    private static Comparator getTimeComparator() {
        return new Comparator<Time>() {

            public int compare(Time d1, Time d2) {
                if (d1.equals(d2))
                    return 0;

                return UP_DOWN * (d1.before(d2) ? 1 : -1);
            }
        };
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getTimestampComparator".
     * 
     * @return
     */
    private static Comparator getTimestampComparator() {

        return new Comparator<Timestamp>() {

            public int compare(Timestamp d1, Timestamp d2) {
                if (d1.equals(d2))
                    return 0;

                return UP_DOWN * (d1.before(d2) ? 1 : -1);
            }
        };
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getLongComparator".
     * 
     * @return
     */
    private static Comparator getLongComparator() {

        return new Comparator<Long>() {

            public int compare(Long arg0, Long arg1) {
                if (arg0 < arg1)
                    return 1 * UP_DOWN;
                if (arg0 > arg1)
                    return -1 * UP_DOWN;
                return 0;
            }
        };
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getDoubleComparator".
     * 
     * @return
     */
    private static Comparator getDoubleComparator() {
        return new Comparator<Double>() {

            public int compare(Double arg0, Double arg1) {
                if (arg0 < arg1)
                    return 1 * UP_DOWN;
                if (arg0 > arg1)
                    return -1 * UP_DOWN;
                return 0;
            }
        };
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getDateComparator".
     * 
     * @return
     */
    private static Comparator getDateComparator() {
        return new Comparator<Date>() {

            public int compare(Date d1, Date d2) {
                if (d1.equals(d2))
                    return 0;

                return UP_DOWN * (d1.before(d2) ? 1 : -1);
            }
        };
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getIntComparator".
     * 
     * @return
     */
    private static Comparator getIntComparator() {
        return new Comparator<Integer>() {

            public int compare(Integer arg0, Integer arg1) {
                if (arg0 < arg1)
                    return 1 * UP_DOWN;
                if (arg0 > arg1)
                    return -1 * UP_DOWN;
                return 0;
            }
        };
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getDefaultComparator".
     * 
     * @return
     */
    private static Comparator<String> getDefaultComparator() {
        return new Comparator<String>() {

            public int compare(String arg0, String arg1) {
                return (col.compare(arg0, arg1)) * UP_DOWN;
            }
        };
    }

}
